package com.wannengbang.cloudleader.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;


import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.ViewLoading;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author:HWC
 */
public class UpdateSettlementDialog extends DialogFragment {
    @BindView(R.id.tv_settle_name)
    EditText tvSettleName;
    @BindView(R.id.tv_settle_mobile)
    EditText tvSettleMobile;
    @BindView(R.id.tv_id_card)
    EditText tvIdCard;
    @BindView(R.id.tv_card_no)
    EditText tvCardNo;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private Dialog dialog;
    private OnCallBackListener onCallBackListener;
    private Activity mActivity;
    private String settle_name;
    private String settle_mobile;
    private String id_card;
    private String card_no;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        initWindow();
        View view = inflater.inflate(R.layout.dialog_updata_settlement, null);
        ButterKnife.bind(this, view);
        mActivity = getActivity();
        initView();
        return view;
    }

    public static UpdateSettlementDialog newInstance(String settle_name, String settle_mobile, String id_card, String card_no) {
        Bundle args = new Bundle();
        UpdateSettlementDialog fragment = new UpdateSettlementDialog();
        args.putString("settle_name", settle_name);
        args.putString("settle_mobile", settle_mobile);
        args.putString("id_card", id_card);
        args.putString("card_no", card_no);
        fragment.setArguments(args);
        return fragment;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    //    初始化请求
    private void initView() {
        settle_name = getArguments().getString("settle_name");
        settle_mobile = getArguments().getString("settle_mobile");
        id_card = getArguments().getString("id_card");
        card_no = getArguments().getString("card_no");

        if (!TextUtils.isEmpty(settle_name) && !TextUtils.isEmpty(settle_mobile) && !TextUtils.isEmpty(id_card)) {
            tvSettleName.setEnabled(false);
            tvSettleMobile.setEnabled(false);
            tvIdCard.setEnabled(false);
            tvSettleName.setTextColor(getResources().getColor(R.color.text_color_3));
            tvSettleMobile.setTextColor(getResources().getColor(R.color.text_color_3));
            tvIdCard.setTextColor(getResources().getColor(R.color.text_color_3));
        }
        tvSettleName.setText(settle_name);
        tvSettleMobile.setText(settle_mobile);
        tvIdCard.setText(id_card);
        tvCardNo.setText(card_no);
    }

    //设置window属性
    private void initWindow() {
        //去掉dialog的标题
        dialog = this.getDialog();
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        Window window = dialog.getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.getAttributes().windowAnimations = R.style.commomDialog;
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        String settleName = tvSettleName.getText().toString();
        String settleMobile = tvSettleMobile.getText().toString();
        String idCard = tvIdCard.getText().toString();
        String cardNo = tvCardNo.getText().toString();

        if (TextUtils.isEmpty(settleName)) {
            ToastUtil.showShort("请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(settleMobile)) {
            ToastUtil.showShort("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(idCard)) {
            ToastUtil.showShort("请输入身份证号");
            return;
        }
        if (TextUtils.isEmpty(cardNo)) {
            ToastUtil.showShort("请输入结算银行卡号");
            return;
        }
        ViewLoading.showProgress(mActivity);
        new HomePageModelImpl().requestVerifyBankFourFactor(settleName, settleMobile, idCard, cardNo, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                onCallBackListener.onCallBack(settleName, settleMobile, idCard, cardNo);
                dismiss();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public interface OnCallBackListener {
        void onCallBack(String settleName, String settleMobile, String idCard, String cardNo);
    }
}
